package com.dream.common;

import com.dream.qcloud.video.api.VideoCloud;
import com.qcloud.PicCloud;
import com.qcloud.PornDetectInfo;
import com.qcloud.UploadResult;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;

import sun.misc.BASE64Encoder;

public class QQImage {
	public static final int APP_ID_V1 = 10006847;
	public static final String SECRET_ID_V1 = "AKID4uJ9VfifItl16Mwy9hnERvAWUO77GA9V";
	public static final String SECRET_KEY_V1 = "NZzTUflNdMC0fWKBuoerbIME36wvtzCy";
	public static final int APP_ID_V2 = 10006847;
	public static final String SECRET_ID_V2 = "AKID4uJ9VfifItl16Mwy9hnERvAWUO77GA9V";
	public static final String SECRET_KEY_V2 = "NZzTUflNdMC0fWKBuoerbIME36wvtzCy";
	public static final String BUCKET = "zhuixun1";
	public static final String TEST_URL = "http://b.hiphotos.baidu.com/image/pic/item/8ad4b31c8701a18b1efd50a89a2f07082938fec7.jpg";

	// 通过控制台获取AppId,SecretId,SecretKey
	public static final int APP_ID = 1000000;
	public static final String SECRET_ID = "SECRET_ID";
	public static final String SECRET_KEY = "SECRET_KEY";

	 
	public static String QQuploadVidoe(String qqurl,String bjurl) throws Exception {
		String result = "";
		try {
			VideoCloud video = new VideoCloud(APP_ID_V1, SECRET_ID_V1, SECRET_KEY_V1);
			 
			String bucketName = "zhuixun1";
			long start = System.currentTimeMillis();
			// 创建目录
			//result = video.createFolder(bucketName, "/zhuixun1/", "");
			//System.out.println("=======createFolder========\n" + result);
			// 上传视频
			result = video.uploadFile(bucketName,qqurl, bjurl);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
	}

	public static String QQupload(byte[] fileStream2) throws Exception {
		String str = null;
		try {
			str = picV2Test(fileStream2);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static void signTest() {
		PicCloud pc = new PicCloud(10006847, "AKID4uJ9VfifItl16Mwy9hnERvAWUO77GA9V", "NZzTUflNdMC0fWKBuoerbIME36wvtzCy", "zhuixun1");
		long expired = System.currentTimeMillis() / 1000L + 3600L;
		String sign = pc.getSign(expired);
		System.out.println("sign=" + sign);
	}

	public static void picV1Test(byte[] fileStream2) throws Exception {
		PicCloud pc = new PicCloud(10006847, "AKID4uJ9VfifItl16Mwy9hnERvAWUO77GA9V", "NZzTUflNdMC0fWKBuoerbIME36wvtzCy");
		picBase(pc, fileStream2);
	}

	public static String picV2Test(byte[] fileStream2) throws Exception {
		PicCloud pc = new PicCloud(10006847, "AKID4uJ9VfifItl16Mwy9hnERvAWUO77GA9V", "NZzTUflNdMC0fWKBuoerbIME36wvtzCy", "zhuixun1");
		return picBase(pc, fileStream2);
	}

	public static String picBase(PicCloud pc, byte[] fileStream2) throws Exception {
		UploadResult result = new UploadResult();

		ByteArrayInputStream inputStream = new ByteArrayInputStream(fileStream2);
		int ret = pc.upload(inputStream, result);
		if (ret == 0) {
			result.print();
		}

		return result.downloadUrl;
	}

	public static void pornTest(String url) {
		PicCloud pc = new PicCloud(10006847, "AKID4uJ9VfifItl16Mwy9hnERvAWUO77GA9V", "NZzTUflNdMC0fWKBuoerbIME36wvtzCy", "zhuixun1");
		PornDetectInfo info = new PornDetectInfo();

		BASE64Encoder be = new BASE64Encoder();

		int ret = pc.pornDetect(url, info);
		if (ret == 0) {
			System.out.println("detect porn pic success");
			info.print();
		} else {
			System.out.println("detect porn pic error, error=" + pc.getError());
		}
	}
}